package com.cai.baselibrary.arouter

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.cai.baselibrary.Constants

/**
 * 做登录拦截
 */
@Interceptor(priority = 100)//拦截等级，数值越大，优先级越高
class LoginInterceptor : IInterceptor {
    private val TAG: String = "LoginInterceptor";

    private var mContext: Context? = null
    override fun init(context: Context?) {
        mContext = context
        LogUtils.i(TAG, "LoginInterceptor 初始化")
    }

    override fun process(postcard: Postcard?, callback: InterceptorCallback) {
        LogUtils.i(TAG, "LoginInterceptor 开始执行")
        postcard?.let {

            val isLogin = SPUtils.getInstance().getBoolean(Constants.isLogin)
            if (isLogin){//如果已经登录，直接打开界面
                callback.onContinue(postcard)
            }else{
                when(it.path){
                    ARouterPath.ACTIVITY_MAIN ->{
                        callback.onContinue(postcard)
                    }

                    ARouterPath.ACTIVITY_LOGIN ->{
                        callback.onContinue(postcard)
                    }

                    else ->{
                        callback.onInterrupt(null)//拦截下来，然后跳转登录
                        Jump.JumpLogin()//跳转登录
                    }

                }
            }


        }




    }

}
